{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c9d6445a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['@startuml\\n',\n",
       " 'skinparam style strictuml\\n',\n",
       " 'skinparam classAttributeIconSize 0\\n',\n",
       " 'skinparam packageBorderStyle dashed\\n',\n",
       " 'left to right direction\\n',\n",
       " '\\n',\n",
       " \"' 用户域\\n\",\n",
       " 'package \"用户域\" {\\n',\n",
       " '  class User as \"用户\" {\\n',\n",
       " '    - 用户ID: Long\\n',\n",
       " '    - 用户名: String\\n',\n",
       " '    - 手机号: long\\n',\n",
       " '    - 余额: double\\n',\n",
       " '  }\\n',\n",
       " '}\\n',\n",
       " '\\n',\n",
       " \"' 论坛域\\n\",\n",
       " 'package \"论坛域\" {\\n',\n",
       " '  enum Visibility as \"可见性\" {\\n',\n",
       " '    公开\\n',\n",
       " '    作者隐藏\\n',\n",
       " '    管理员隐藏\\n',\n",
       " '  }\\n',\n",
       " '  class Blog as \"博客\" {\\n',\n",
       " '    - 博客ID: Long\\n',\n",
       " '    - 发布者ID: Long\\n',\n",
       " '    - 标题: String\\n',\n",
       " '    - 内容: String\\n',\n",
       " '    - 发布时间: LocalDateTime\\n',\n",
       " '    - 可见性: Visibility\\n',\n",
       " '  }\\n',\n",
       " '  class Reply as \"回复\" {\\n',\n",
       " '    - 回复ID: Long\\n',\n",
       " '    - 所属博客ID: Long\\n',\n",
       " '    - 发送者ID: Long\\n',\n",
       " '    - 内容: String\\n',\n",
       " '    - 发送时间: LocalDateTime\\n',\n",
       " '    - 父回复ID: Long\\n',\n",
       " '  }\\n',\n",
       " '  class BlockTag as \"引用标签\" {\\n',\n",
       " '    - 标签ID: Long\\n',\n",
       " '    - 博客ID: Long\\n',\n",
       " '    - 标签内容: String\\n',\n",
       " '  }\\n',\n",
       " '  博客 \"1\" -- \"0..*\" 引用标签 : 包含\\n',\n",
       " '}\\n',\n",
       " '\\n',\n",
       " 'User \"1\" -- \"0..*\" 博客 : 发布\\n',\n",
       " '博客 \"1\" -- \"0..*\" 回复 : 包含\\n',\n",
       " '\\n',\n",
       " \"' 跑腿域\\n\",\n",
       " 'package \"跑腿域\" {\\n',\n",
       " '  enum ErrandOrderStatus as \"订单状态\" {\\n',\n",
       " '    待接单\\n',\n",
       " '    已接单\\n',\n",
       " '    已完成\\n',\n",
       " '    已确认\\n',\n",
       " '    已过期\\n',\n",
       " '    已取消\\n',\n",
       " '  }\\n',\n",
       " '  class Errand as \"跑腿订单\" {\\n',\n",
       " '    - 跑腿ID: Long\\n',\n",
       " '    - 发布者ID: Long\\n',\n",
       " '    - 标题: String\\n',\n",
       " '    - 描述: String\\n',\n",
       " '    - 费用: Double\\n',\n",
       " '    - 起点: String\\n',\n",
       " '    - 终点: String\\n',\n",
       " '    - 最晚送达时间: LocalDateTime\\n',\n",
       " '    - 接单者ID: Long\\n',\n",
       " '    - 状态: ErrandOrderStatus\\n',\n",
       " '  }\\n',\n",
       " '  订单状态 .down. 跑腿订单\\n',\n",
       " '}\\n',\n",
       " 'User \"1\" -- \"0..*\" 跑腿订单 : 发布\\n',\n",
       " '\\n',\n",
       " \"' 商城域\\n\",\n",
       " 'package \"商城域\" {\\n',\n",
       " '  class Shop as \"商铺\" {\\n',\n",
       " '    - 商铺ID: Long\\n',\n",
       " '    - 名称: String\\n',\n",
       " '    - 描述: String\\n',\n",
       " '  }\\n',\n",
       " '  class Good as \"商品\" {\\n',\n",
       " '    - 商品ID: Long\\n',\n",
       " '    - 所属商铺ID: Long\\n',\n",
       " '    - 名称: String\\n',\n",
       " '    - 价格: Double\\n',\n",
       " '  }\\n',\n",
       " '  class ShopOrder as \"商铺订单\" {\\n',\n",
       " '    - 订单ID: Long\\n',\n",
       " '    - 买家ID: Long\\n',\n",
       " '    - 商铺ID: Long\\n',\n",
       " '  }\\n',\n",
       " '  class GoodTransaction as \"交易记录\" {\\n',\n",
       " '    - 记录ID: Long\\n',\n",
       " '    - 商品ID: Long\\n',\n",
       " '    - 所属订单ID: Long\\n',\n",
       " '    - 数量: Long\\n',\n",
       " '  }\\n',\n",
       " '  GoodTransaction .down. ShopOrder\\n',\n",
       " '}\\n',\n",
       " 'User \"1\" -- \"0..*\" ShopOrder : 下单\\n',\n",
       " 'Shop \"1\" -- \"0..*\" Good : 销售\\n',\n",
       " 'GoodTransaction \"1\" -- \"1\" Good : 针对\\n',\n",
       " '\\n',\n",
       " \"' 充值域\\n\",\n",
       " 'package \"充值域\" {\\n',\n",
       " '  class Charge as \"充值记录\" {\\n',\n",
       " '    - 记录ID: Long\\n',\n",
       " '    - 用户ID: Long\\n',\n",
       " '    - 金额: double\\n',\n",
       " '    - 充值时间: LocalDateTime\\n',\n",
       " '  }\\n',\n",
       " '}\\n',\n",
       " 'User \"1\" -- \"0..*\" 充值记录 : 充值\\n',\n",
       " '\\n',\n",
       " \"' 投诉域\\n\",\n",
       " 'package \"投诉域\" {\\n',\n",
       " '  enum ComplaintBlock as \"投诉板块\" {\\n',\n",
       " '    论坛\\n',\n",
       " '    跑腿\\n',\n",
       " '    商城\\n',\n",
       " '  }\\n',\n",
       " '  class Complaint as \"投诉\" {\\n',\n",
       " '    - 投诉ID: Long\\n',\n",
       " '    - 投诉者ID: Long\\n',\n",
       " '    - 板块: ComplaintBlock\\n',\n",
       " '    - 被投诉对象ID: Long\\n',\n",
       " '    - 标题: String\\n',\n",
       " '    - 描述: String\\n',\n",
       " '  }\\n',\n",
       " '  Complaint .down. ComplaintBlock\\n',\n",
       " '  enum BanBlock as \"封禁板块\"{\\n',\n",
       " '    论坛\\n',\n",
       " '    跑腿\\n',\n",
       " '  }\\n',\n",
       " '  class Ban as \"封禁\" {\\n',\n",
       " '    - 用户ID: Long\\n',\n",
       " '    - 板块: BanBlock\\n',\n",
       " '    - 解封时间: LocalDateTime\\n',\n",
       " '  }\\n',\n",
       " '  Ban .down. BanBlock\\n',\n",
       " '}\\n',\n",
       " 'User \"1\" -- \"0..*\" Complaint : 投诉\\n',\n",
       " 'User \"1\" -- \"0..*\" Ban : 被封禁\\n',\n",
       " '\\n',\n",
       " '@enduml\\n']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp=open(\"2.2.puml\")\n",
    "raw=fp.readlines()\n",
    "fp.close()\n",
    "raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "41ba9995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['  class User as \"用户\" {\\n',\n",
       " '  enum Visibility as \"可见性\" {\\n',\n",
       " '  class Blog as \"博客\" {\\n',\n",
       " '  class Reply as \"回复\" {\\n',\n",
       " '  class BlockTag as \"引用标签\" {\\n',\n",
       " '  enum ErrandOrderStatus as \"订单状态\" {\\n',\n",
       " '  class Errand as \"跑腿订单\" {\\n',\n",
       " '  class Shop as \"商铺\" {\\n',\n",
       " '  class Good as \"商品\" {\\n',\n",
       " '  class ShopOrder as \"商铺订单\" {\\n',\n",
       " '  class GoodTransaction as \"交易记录\" {\\n',\n",
       " '  class Charge as \"充值记录\" {\\n',\n",
       " '  enum ComplaintBlock as \"投诉板块\" {\\n',\n",
       " '  class Complaint as \"投诉\" {\\n',\n",
       " '  enum BanBlock as \"封禁板块\"{\\n',\n",
       " '  class Ban as \"封禁\" {\\n']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rep_candidates=[\n",
    "    l for l in raw\n",
    "    if (\"class\" in l or \"enum \"in l or \"interface \" in l)\n",
    "    and \"as\" in l\n",
    "    and '\\\"' in l\n",
    "]\n",
    "rep_candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "417c0147",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'en': 'User', 'zh': '用户'},\n",
       " {'en': 'enumVisibility', 'zh': '可见性'},\n",
       " {'en': 'Blog', 'zh': '博客'},\n",
       " {'en': 'Reply', 'zh': '回复'},\n",
       " {'en': 'BlockTag', 'zh': '引用标签'},\n",
       " {'en': 'enumErrandOrderStatus', 'zh': '订单状态'},\n",
       " {'en': 'Errand', 'zh': '跑腿订单'},\n",
       " {'en': 'Shop', 'zh': '商铺'},\n",
       " {'en': 'Good', 'zh': '商品'},\n",
       " {'en': 'ShopOrder', 'zh': '商铺订单'},\n",
       " {'en': 'GoodTransaction', 'zh': '交易记录'},\n",
       " {'en': 'Charge', 'zh': '充值记录'},\n",
       " {'en': 'enumComplaintBlock', 'zh': '投诉板块'},\n",
       " {'en': 'Complaint', 'zh': '投诉'},\n",
       " {'en': 'enumBanBlock', 'zh': '封禁板块'},\n",
       " {'en': 'Ban', 'zh': '封禁'}]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rep_dicts=[\n",
    "    {\n",
    "        \"en\":l.split(\"as \")[0].replace(\"\\n\",\"\").replace(\"class \",\"\").replace(\" \",\"\"),\n",
    "        \n",
    "        \"zh\":l.split(\"as \")[1].replace(\"\\n\",\"\").replace(\"\\\"\",\"\").replace(\"{\",\"\").replace(\" \",\"\")\n",
    "    }\n",
    "    for l in rep_candidates\n",
    "]\n",
    "rep_dicts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "dd2b8b24",
   "metadata": {},
   "outputs": [],
   "source": [
    "out=[]\n",
    "for l in raw:\n",
    "    append_buf=[l]\n",
    "    if \"\\\" -- \\\"\"in append_buf[0] or \".down\" in append_buf[0]:\n",
    "        for rep_dict in rep_dicts:\n",
    "            if rep_dict[\"zh\"] in append_buf[0]:\n",
    "                append_buf[0]=l.replace(rep_dict[\"zh\"],rep_dict[\"en\"])\n",
    "                break\n",
    "    \n",
    "    out.append(append_buf[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "3e2a4da3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'@startuml\\nskinparam style strictuml\\nskinparam classAttributeIconSize 0\\nskinparam packageBorderStyle dashed\\nleft to right direction\\n\\n\\' 用户域\\npackage \"用户域\" {\\n  class User as \"用户\" {\\n    - 用户ID: Long\\n    - 用户名: String\\n    - 手机号: long\\n    - 余额: double\\n  }\\n}\\n\\n\\' 论坛域\\npackage \"论坛域\" {\\n  enum Visibility as \"可见性\" {\\n    公开\\n    作者隐藏\\n    管理员隐藏\\n  }\\n  class Blog as \"博客\" {\\n    - 博客ID: Long\\n    - 发布者ID: Long\\n    - 标题: String\\n    - 内容: String\\n    - 发布时间: LocalDateTime\\n    - 可见性: Visibility\\n  }\\n  class Reply as \"回复\" {\\n    - 回复ID: Long\\n    - 所属博客ID: Long\\n    - 发送者ID: Long\\n    - 内容: String\\n    - 发送时间: LocalDateTime\\n    - 父回复ID: Long\\n  }\\n  class BlockTag as \"引用标签\" {\\n    - 标签ID: Long\\n    - 博客ID: Long\\n    - 标签内容: String\\n  }\\n  Blog \"1\" -- \"0..*\" 引用标签 : 包含\\n}\\n\\nUser \"1\" -- \"0..*\" Blog : 发布\\nBlog \"1\" -- \"0..*\" 回复 : 包含\\n\\n\\' 跑腿域\\npackage \"跑腿域\" {\\n  enum ErrandOrderStatus as \"订单状态\" {\\n    待接单\\n    已接单\\n    已完成\\n    已确认\\n    已过期\\n    已取消\\n  }\\n  class Errand as \"跑腿订单\" {\\n    - 跑腿ID: Long\\n    - 发布者ID: Long\\n    - 标题: String\\n    - 描述: String\\n    - 费用: Double\\n    - 起点: String\\n    - 终点: String\\n    - 最晚送达时间: LocalDateTime\\n    - 接单者ID: Long\\n    - 状态: ErrandOrderStatus\\n  }\\n  enumErrandOrderStatus .down. 跑腿订单\\n}\\nUser \"1\" -- \"0..*\" Errand : 发布\\n\\n\\' 商城域\\npackage \"商城域\" {\\n  class Shop as \"商铺\" {\\n    - 商铺ID: Long\\n    - 名称: String\\n    - 描述: String\\n  }\\n  class Good as \"商品\" {\\n    - 商品ID: Long\\n    - 所属商铺ID: Long\\n    - 名称: String\\n    - 价格: Double\\n  }\\n  class ShopOrder as \"商铺订单\" {\\n    - 订单ID: Long\\n    - 买家ID: Long\\n    - 商铺ID: Long\\n  }\\n  class GoodTransaction as \"交易记录\" {\\n    - 记录ID: Long\\n    - 商品ID: Long\\n    - 所属订单ID: Long\\n    - 数量: Long\\n  }\\n  GoodTransaction .down. ShopOrder\\n}\\nUser \"1\" -- \"0..*\" ShopOrder : 下单\\nShop \"1\" -- \"0..*\" Good : 销售\\nGoodTransaction \"1\" -- \"1\" Good : 针对\\n\\n\\' 充值域\\npackage \"充值域\" {\\n  class Charge as \"充值记录\" {\\n    - 记录ID: Long\\n    - 用户ID: Long\\n    - 金额: double\\n    - 充值时间: LocalDateTime\\n  }\\n}\\nUser \"1\" -- \"0..*\" Charge : 充值\\n\\n\\' 投诉域\\npackage \"投诉域\" {\\n  enum ComplaintBlock as \"投诉板块\" {\\n    论坛\\n    跑腿\\n    商城\\n  }\\n  class Complaint as \"投诉\" {\\n    - 投诉ID: Long\\n    - 投诉者ID: Long\\n    - 板块: ComplaintBlock\\n    - 被投诉对象ID: Long\\n    - 标题: String\\n    - 描述: String\\n  }\\n  Complaint .down. ComplaintBlock\\n  enum BanBlock as \"封禁板块\"{\\n    论坛\\n    跑腿\\n  }\\n  class Ban as \"封禁\" {\\n    - 用户ID: Long\\n    - 板块: BanBlock\\n    - 解封时间: LocalDateTime\\n  }\\n  Ban .down. BanBlock\\n}\\nUser \"1\" -- \"0..*\" Complaint : Complaint\\nUser \"1\" -- \"0..*\" Ban : 被Ban\\n\\n@enduml\\n'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "out_str=\"\".join(out)\n",
    "out_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ef1af3c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "fp=open(\"2.2.generated.puml\",\"w\")\n",
    "fp.write(out_str)\n",
    "fp.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pip_main",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
